var apireadyMap = [initPage];
var pagenext = 1;
var data_opt_volist = dataOptVolist();

function initPage() {
    dropload.config({
        scrollRange: 10,
        htmlNothing: function() {
            return '';
        },
        callback: selfVolist.init
    });
    dropload.init(document.getElementById('dropView'));
    dropload.loading();
    selfVolist.init();

    // 开奖历史 号码|大小|单双
    $('.layout-tab-head .layout-tab-item').on('click', function(e) {
        var that = $(this);
        var status = that.attr("data-status");
        that.addClass('active').siblings().removeClass('active');
        if(status == 'all') {
            delete data_opt_volist['status'];
        } else {
            data_opt_volist['status'] = status;
        }
        pagenext = 1;
        $('#databaseVolist').html('').removeClass('nothing');
        dropload.loading();
        selfVolist.init();
    });

    // 默认显示投注
    // $('.layout-tab-head .layout-tab-item[data-cmd="num"]').trigger('click');

    $(document).on('click', '.btnCmd', function(e) {
        var that = $(this);
        var data_opt = {
            id: that.attr('data-id'),
            status: that.attr('data-status')
        };
        changeSelfDatabase.init(data_opt);
    });
}

// 私人定制列表
var selfVolist = {
    init: function() {
        var self = selfVolist;
        data_opt_volist['page'] = pagenext;
        ajax.get('/api?ctrl.self.volist', data_opt_volist, function(res) {
            if(res.state) {
                pagenext = res.data.next;
                if(res.data.list.length > 0) {
                    self.render(res.data.list);
                } else {
                    $('#databaseVolist').addClass('nothing');
                }
                if(res.data.next > 0) {
                    dropload.resetLoad();
                } else {
                    dropload.noData();
                }
            } else {
                // 请求失败
                setTimeout(self.init, 800);
            }
        });
    },
    render: function(data) {
        var ele = '';
        for(var i in data) {
            var list = data[i];
            ele += '<div class="itemBox">';
            ele += '<div class="dis-table">';
            ele += '<div class="dis-row">';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">定制账号</font>';
            ele += '<font class="value" id="ipt-mobile">' + list.mobile + '</font>';
            ele += '</div>';
            ele += '<div class="dis-cell seize"> </div>';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">表单状态</font>';
            ele += '<font class="value" id="ipt-mobile">' + formatStatus(list.status) + '</font>';
            ele += '</div>';
            ele += '</div>';
            ele += '<div class="dis-row">';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">联系手机</font>';
            ele += '<font class="value" id="ipt-mobile">' + formatDec(list.f_mobile) + '</font>';
            ele += '</div>';
            ele += '<div class="dis-cell seize"> </div>';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">联系微信</font>';
            ele += '<font class="value" id="ipt-mobile">' + formatDec(list.f_wechat) + '</font>';
            ele += '</div>';
            ele += '</div>';
            ele += '<div class="dis-row">';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">提交时间</font>';
            ele += '<font class="value" id="ipt-mobile">' + formatDatetime(list.create_time) + '</font>';
            ele += '</div>';
            ele += '<div class="dis-cell seize"> </div>';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">受理时间</font>';
            ele += '<font class="value" id="ipt-mobile">' + formatDatetime(list.update_time) + '</font>';
            ele += '</div>';
            ele += '</div>';
            ele += '<div class="dis-row">';
            ele += '<div class="dis-cell solid-bottom">';
            ele += '<font class="label solid-right">完成时间</font>';
            ele += '<font class="value" id="ipt-mobile">' + formatDatetime(list.delete_time) + '</font>';
            ele += '</div>';
            ele += '<div class="dis-cell seize"> </div>';
            ele += '<div class="dis-cell text-center">';
            if(list.status == 0) {
                ele += '<div class="dis-table btnCmdBox">';
                ele += '<p class="dis-cell"><a href="javascript:;" class="btnCmd" data-id="' + list.id + '" data-status="1" style="color: green;">受理</a></p>';
                ele += '<p class="dis-cell solid-left"><a href="javascript:;" class="btnCmd" data-id="' + list.id + '" data-status="9" style="color: red;">废弃</a></p>';
                ele += '</div>';
                /*ele += '<a href="javascript:;" class="p-x-1x btnCmd" data-status="1" style="color: green;">受理</a>';
                ele += '<a href="javascript:;" class="p-x-1x btnCmd" data-status="9" style="color: red;">废弃</a>';*/
            } else if(list.status == 1) {
                ele += '<a href="javascript:;" class="btnCmd" data-id="' + list.id + '" data-status="2" style="color: green;">完结</a>';
            } else {
                ele += '';
            }
            ele += '</div>';
            ele += '</div>';
            ele += '</div>';
            ele += '<div class="content"><textarea readonly="readonly">' + list.f_content + '</textarea></div>';
            ele += '</div>';
        }
        $('#databaseVolist').append(ele);
    }
}

function dataOptVolist(opts) {
    return tool.optionExtend({
        'page': 1, // 当前页数
        'show': 10 // 显示条数
    }, opts || {});
}

function formatDec(value) {
    if(value == '') {
        return '--';
    } else {
        return value;
    }
}

function formatDatetime(time) {
    if(Number(time) > 0) {
        return tool.formatDate('yy-mm-dd', time);
    } else {
        return '--';
    }
}

function formatStatus(status) {
    var opt = {
        0: '待受理',
        1: '已受理',
        2: '已完结',
        9: '废弃'
    };
    return opt[status];
}

// 改变状态
var changeSelfDatabase = {
    init: function(data_opt) {
        var self = changeSelfDatabase;
        var modalId = modal.load.show();
        ajax.put('/api?ctrl.self.status', data_opt, function(res) {
            modal.load.hide(modalId);
            if (res.state) {
                self.render(res.data);
            } else {
                modal.tips(res.error || '响应超时，请重新请求');
            }
        });
    },
    render: function(data) {
        modal.tips('状态修改成功', function() {
            window.location.reload();
        })
    }
};